<?php

class Diggme
{
    public $appKey = '';
    public $appSecret = '';
    public $curl;
    public $token = '';
    public $header;
    public $serverUrl = 'https://api.diggme.cn';

    public function __construct($appKey, $appSecret)
    {
        $this->appKey = $appKey;
        $this->appSecret = $appSecret;
        $this->curl = new Curl();
    }

    /**
     * 获取token
     * @return mixed
     */
    public function getToken()
    {
        $url = $this->serverUrl.'/api/v1/open/token';
        $params = [
            'grant_type' => 'client_credential',
            'app_key' => $this->appKey,
            'app_secret' => $this->appSecret,
        ];

        $response = $this->curl->get($url, $params);
        $resData = json_decode($response, true);
        if ($resData['status'] == true) {
            $this->token = $resData['data']['access_token'];
            $this->header = ['Authorization: '.$this->token];
        };

        return $response;
    }

    public function getCategoryList()
    {
        $url = $this->serverUrl.'/api/v1/channel/test/category/list';
        $params = [];

        $this->curl->setHeader($this->header);
        return $this->curl->get($url, $params);
    }

    /**
     * 获取测试列表
     * @param int $categoryId
     * @param int $page
     * @param int $size
     * @return mixed
     */
    public function getTestList($categoryId = 0, $page = 1, $size = 20)
    {
        $url = $this->serverUrl.'/api/v1/channel/test/list';
        $params = [
            'category_id' => $categoryId,
            'page' => $page,
            'size' => $size,
        ];

        $this->curl->setHeader($this->header);
        return $this->curl->get($url, $params);

    }

    /**
     * 获取测试详情
     * @param $testId
     * @return mixed
     */
    public function getTestDetail($testId)
    {
        $url = $this->serverUrl.'/api/v1/channel/test/detail';
        $params = [
            'test_id' => $testId,
        ];

        $this->curl->setHeader($this->header);
        return $this->curl->get($url, $params);
    }

    /**
     * 获取测试订单兑换码(in_code)
     * @param $testId
     * @param $orderNO
     * @return mixed
     */
    public function getTestCode($testId, $orderNO)
    {
        $url = $this->serverUrl.'/api/v1/channel/test/code';
        $params = [
            'test_id' => $testId,
            'no' => $orderNO
        ];

        $this->curl->setHeader($this->header);
        return $this->curl->get($url, $params);
    }

    /**
     * 查询测试兑换码(in_code)状态
     * @param $testId
     * @param $inCode
     * @return mixed
     */
    public function getCodeStatus($testId, $inCode)
    {
        $url = $this->serverUrl.'/api/v1/channel/test/codeStatus';
        $params = [
            'test_id' => $testId,
            'in_code' => $inCode
        ];

        $this->curl->setHeader($this->header);
        return $this->curl->get($url, $params);
    }

    /**
     * 订单状态回调
     * @param $inCode
     * @return mixed
     */
    public function postNotifyPay($inCode)
    {
        $url = $this->serverUrl.'/api/v1/channel/notify/payCb';
        $encryptData['in_code'] = $inCode;
        $encryptData['status'] = 'success';

        $params = [
            'encrypt_data' => @openssl_encrypt(json_encode($encryptData),'aes-256-cfb', $this->appSecret)
        ];

        $this->curl->setHeader($this->header);
        return $this->curl->get($url, $params);
    }

}